const fs = require('fs');
const path = require('path');

const srcDir = path.join(__dirname, 'src');
const excludeDir = path.join(srcDir, 'static', 'locales');
const report = [];
const validExts = ['.ts', '.js', '.vue', '.json', '.scss', '.css', '.html'];

function isInLocales(filePath) {
  return filePath.startsWith(excludeDir);
}

// 块注释 /* ... */
const BLOCK_COMMENT_RE = /\/\*[\s\S]*?\*\//g;
// 行注释 // ... （注意避免误删 http://）
const LINE_COMMENT_RE = /(^|[^:])\/\/[^\r\n]*/g;

function removeBlockComments(str) {
  return str.replace(BLOCK_COMMENT_RE, '');
}

function removeLineComments(str) {
  return str.replace(LINE_COMMENT_RE, '$1'); // 保留前导符号，避免删掉 http://
}
function removeChinese(str) {
  return str.replace(/[\u4e00-\u9fa5]/g, '');
}

function removeHtmlComments(str) {
  return str.replace(/<!--[\s\S]*?-->/g, '');
}

function processFile(filePath) {
  if (isInLocales(filePath)) return;
  const ext = path.extname(filePath);
  if (!validExts.includes(ext)) return;
  const lines = fs.readFileSync(filePath, 'utf8').split(/\r?\n/);
  let changed = false;
  const newLines = lines.map((line, idx) => {
    let originalLine = line;

    // Remove Chinese
    const chineseMatch = line.match(/[\u4e00-\u9fa5]+/g);
    if (chineseMatch) {
      line = removeChinese(line);
      changed = true;
      report.push({
        file: filePath.replace(__dirname + path.sep, ''),
        line: idx + 1,
        type: 'Chinese',
        removed: chineseMatch.join(''),
        original: originalLine
      });
    }
	const blockMatch = originalLine.match(BLOCK_COMMENT_RE);
	if (blockMatch) {
	  line = removeBlockComments(line);
	  changed = true;
	  report.push({
	    file: filePath.replace(__dirname + path.sep, ''),
	    line: idx + 1,
	    type: 'BlockComment',
	    removed: blockMatch.join(''),
	    original: originalLine
	  });
	  originalLine = line;
	}
	
	// Remove Line comments // ...
	const lineMatch = originalLine.match(LINE_COMMENT_RE);
	if (lineMatch) {
	  line = removeLineComments(line);
	  changed = true;
	  report.push({
	    file: filePath.replace(__dirname + path.sep, ''),
	    line: idx + 1,
	    type: 'LineComment',
	    removed: lineMatch.join(''),
	    original: originalLine
	  });
	}

    // Remove HTML comments
    const commentMatch = originalLine.match(/<!--[\s\S]*?-->/g);
    if (commentMatch) {
      line = removeHtmlComments(line);
      changed = true;
      report.push({
        file: filePath.replace(__dirname + path.sep, ''),
        line: idx + 1,
        type: 'HTMLComment',
        removed: commentMatch.join(''),
        original: originalLine
      });
    }

    return line;
  });

  if (changed) {
    fs.writeFileSync(filePath, newLines.join('\n'), 'utf8');
  }
}

function walkDir(dir) {
  fs.readdirSync(dir).forEach(file => {
    const fullPath = path.join(dir, file);
    if (fs.statSync(fullPath).isDirectory()) {
      if (fullPath === excludeDir) return;
      walkDir(fullPath);
    } else {
      processFile(fullPath);
    }
  });
}

walkDir(srcDir);
fs.writeFileSync('report.json', JSON.stringify(report, null, 2), 'utf8');
console.log('Chinese characters and HTML comments have been deleted. See report.json for details.');
